﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight.Messaging;
using Medecine.Interface.Users;
using System.Windows;
using System.Windows.Media.Animation;
using Medecine.Interface.Patients;
using Medecine.Interface.Observations;

namespace Medecine.Interface.Main
{
	/// <summary>
	/// Singleton to handle navigation between the views.
	/// </summary>
	public class ViewController
	{
		private ViewController()
		{
		}

		#region Singleton

		private static ViewController _instance = new ViewController();
		public static ViewController Instance { get { return _instance; } }

		#endregion // Singleton

		public void Initialize(MainView mainView)
		{
			_mainView = mainView;
			Messenger.Default.Register<GoToViewMessage>(this, GoToView);
			Messenger.Default.Register<ErrorMessage>(this, DisplayError);
		}

		#region Main view

		private MainView _mainView;

		#endregion // Main view

		private ViewName _currentView = ViewName.Login;

		private void GoToView(GoToViewMessage message)
		{
			switch (message.ViewName)
			{
				case ViewName.Home:
					GoToHome(message);
					break;
				case ViewName.Login:
					_mainView.GoToLogin();
					break;
				case ViewName.AddUser:
					GoToAddUser(message);
					break;
				case ViewName.AddPatient:
					GoToAddPatient(message);
					break;
				case ViewName.AddObservation:
					GoToAddObservation(message);
					break;
				default:
					throw new NotImplementedException("GoToView case not handled.");
				//break;
			}
			_currentView = message.ViewName;
		}

		private void GoToHome(GoToViewMessage message)
		{
			if (_currentView == ViewName.Login)
				_mainView.GoToLogged();
		}

		#region Add user

		private void GoToAddUser(GoToViewMessage message)
		{
			AddUserView view = new AddUserView();
			view.DataContext = message.ViewModel;
			Window _addUserWindow = new Window();
			_addUserWindow.Content = view;
			_addUserWindow.SizeToContent = SizeToContent.WidthAndHeight;
			_addUserWindow.Owner = _mainView;
			_addUserWindow.ShowDialog();
			message.Callback();
		}

		#endregion // Add user

		#region Add patient

		private void GoToAddPatient(GoToViewMessage message)
		{
			AddPatientView view = new AddPatientView();
			view.DataContext = message.ViewModel;
			Window _addPatientWindow = new Window();
			_addPatientWindow.Content = view;
			_addPatientWindow.SizeToContent = SizeToContent.WidthAndHeight;
			_addPatientWindow.Owner = _mainView;
			_addPatientWindow.ShowDialog();
			message.Callback();
		}

		#endregion // Add patient

		#region Add observation

		private void GoToAddObservation(GoToViewMessage message)
		{
			AddObservationView view = new AddObservationView();
			view.DataContext = message.ViewModel;
			Window _addObservationWindow = new Window();
			_addObservationWindow.Content = view;
			_addObservationWindow.SizeToContent = SizeToContent.WidthAndHeight;
			_addObservationWindow.Owner = _mainView;
			_addObservationWindow.ShowDialog();
			message.Callback();
		}

		#endregion // Add observation

		#region Errors

		private void DisplayError(ErrorMessage message)
		{
			MessageBox.Show(message.Notification, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
		}

		#endregion // Errors
	}
}
